package com.example.commonmistak.designpatterns.template;

import com.example.commonmistak.designpatterns.template.db.Db;
import com.example.commonmistak.designpatterns.template.entity.Cart;
import com.example.commonmistak.designpatterns.template.service.AbstractCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xiongwei
 * @date 2022/1/14 11:35
 */
public class DemoController {

    private Map<Long, Integer> items = new HashMap(){{
        items.put(1L, 2);
        items.put(2L, 4);
    }};



    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("demo")
    public Cart demo(int userId) {
        String userCategory = Db.getUserCategory(userId);
        //简单工厂模式
        AbstractCart cart = (AbstractCart) applicationContext.getBean(userCategory + "UserCart");
        return cart.process(userId, items);
    }
}
